
IddTankRespawnDialog = 1230;
IdcCbHumanRespawnList = 1231;
IdcTankMap= 1232;
IdcBtMatrixInsert =1233;
IddTankDialog = 1230;
lastDataSelected = nil;

HumanRespawnList=[];

FireEvents={
    ctrlEnable [IdcBtMatrixInsert,true];
    lastDataSelected=lbText[IdcCbHumanRespawnList, lbCurSel IdcCbHumanRespawnList];
    player globalchat format[" select : %1 %2",lastDataSelected,lbCurSel IdcCbHumanRespawnList];        

};

BtMatrixInsert_click={
	player globalChat format ["test bt"];
	_selectedHuman=nil;
	_value=lbCurSel IdcCbHumanRespawnList;
	player globalChat format ["_value = %1",_value]; 
	_selectedHuman=HumanRespawnList select _value;
	player globalChat format ["_selectedHuman = %1",_selectedHuman]; 
	
}; 

//cutRsc ["Default" ,"BLACK",2];

endDialog=false;

_lastHumanNb=0;

while {true} do{

    scopeName "dialogLoop";

    _TankRespawnDialog = createDialog "TankRespawnDialog";
    _TankDialog=findDisplay IddTankDialog;
    CbHumanRespawnList = (_TankDialog displayCtrl IdcCbHumanRespawnList);
	BtMatrixInsert = (_TankDialog displayCtrl IdcBtMatrixInsert);
	
    CbHumanRespawnList ctrlSetEventHandler ["LBSelChanged","['ListChange',_this] call FireEvents"];
    BtMatrixInsert ctrlSetEventHandler ["ButtonClick","['',_this] call BtMatrixInsert_click"];
	
    ctrlEnable [IdcBtMatrixInsert,false];

    while {dialog && !endDialog} do 
	{

        //TankPlayer globalChat format["test %1",list trgRespawn];

        
        //TankPlayer globalChat format["waitUntil %1 != %2",count (list trgRespawn),_lastHumanNb];

        waitUntil { count (list trgRespawn) != _lastHumanNb };

        _lastHumanNb= count (list trgRespawn);

        lbClear IdcCbHumanRespawnList;	

		HumanRespawnList=[];
		
        { // foreach human in respawn zone

            _human = _x;
			HumanRespawnList=HumanRespawnList+[_human];
            _index=lbAdd [IdcCbHumanRespawnList,name _human];
            lbSetData [IdcCbHumanRespawnList, _index, _human];
            //TankPlayer addAction [ , "TankHumanPlacement.sqf" , [_human] ];

        }forEach(list trgRespawn);


		// reselect the element selected before update 
		for [{_x=0}, {_x<(lbSize IdcCbHumanRespawnList)}, {_x=_x+1}] do
		{
			scopeName "reselectLoop";

			 //player globalchat format[" test %1 == %2 , %3 ", lastDataSelected,lbText[IdcCbHumanRespawnList,_x],_x];
			
			if (lbText[IdcCbHumanRespawnList,_x] == lastDataSelected) then 
			{
				lbSetCurSel [IdcCbHumanRespawnList, _x];
				//player globalchat "-------EQUAL---------";
				breakOut "reselectLoop";
			};


		 };

        //sleep 1;
    };
    
    if(endDialog)then{
       sleep 4;
       breakOut "dialogLoop";
    };
};